package com.first.homeqian.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.first.common.pojo.EduCourse;
import com.first.common.pojo.EduSubject;
import com.first.common.pojo.TbZone;
import com.first.common.pojo.TbZoneCourse;
import com.first.common.vo.Result;
import com.first.homeqian.mapper.SubjectMapper;
import com.first.homeqian.mapper.ZoneMapper;
import com.first.homeqian.service.ZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: first-project
 * @description:
 * @author: 闫锦超
 * @create: 2023-06-01 16:45
 **/
@Service
public class ZoneServiceImpl implements ZoneService {
    @Autowired
    private ZoneMapper zoneMapper;
    @Autowired
    private SubjectMapper subjectMapper;



    @Override
    public Result getAllQianZone() {
        QueryWrapper<TbZone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_disable",0);
        wrapper.eq("is_deleted",0);
        List<TbZone> tbZones = zoneMapper.selectList(wrapper);
        QueryWrapper<TbZoneCourse> courseWrapper = new QueryWrapper<>();
        for (TbZone tbZone : tbZones) {
            courseWrapper.clear();
            courseWrapper.eq("tzc.is_disable",0);
            courseWrapper.eq("tzc.is_deleted",0);
            courseWrapper.eq("ec.status","Normal");
            courseWrapper.eq("tzc.zone_id",tbZone.getId());
            List<EduCourse> zoneAllCourse = zoneMapper.findZoneAllCourse(courseWrapper);
            tbZone.setCourseList(zoneAllCourse);
        }
        return Result.success(tbZones);
    }

    @Override
    public Result getAllQianMenu() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("status_id",1);
        wrapper.eq("parent_id",0);
        List<EduSubject> eduSubjects = subjectMapper.selectList(wrapper);
        QueryWrapper<EduSubject> childrenWrapper = new QueryWrapper<>();
        childrenWrapper.eq("status_id",1);
        List<EduSubject> list = subjectMapper.selectList(childrenWrapper);
        for (EduSubject eduSubject : eduSubjects) {
            List<EduSubject> children = getChildren(eduSubject, list);
            eduSubject.setChildren(children);
        }
        return Result.success(eduSubjects);
    }
    private List<EduSubject> getChildren(EduSubject eduSubject,List<EduSubject> list){
        ArrayList<EduSubject> eduSubjects = new ArrayList<>();
        for (EduSubject subject : list) {
            if(eduSubject.getId().equals(subject.getParentId())){
                eduSubjects.add(subject);
            }
        }
        for (EduSubject subject : eduSubjects) {
            subject.setChildren(getChildren(subject,list));
        }
        return eduSubjects;
    }
}
